﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
using System.IO;

namespace SimpleApp.Controllers
{
    public class JSONHelperController : Controller
    {
        public static StringBuilder resultClass = new StringBuilder();

        public ActionResult index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Save()
        {

            StringBuilder strSaved = new StringBuilder();

            strSaved.Append("using System;\r\n");
            strSaved.Append("using System.Collections.Generic;\r\n");
            strSaved.Append("using System.Linq;\r\n");
            strSaved.Append("using System.Web;\r\n");
            strSaved.Append("\r\n");
            strSaved.Append("namespace SimpleApp.Models\r\n");
            strSaved.Append("{\r\n");
            strSaved.Append(resultClass);
            strSaved = strSaved.Replace("<br/>", "\r\n");
            strSaved = strSaved.Replace("&nbsp;", " ");
            strSaved = strSaved.Replace("&lt;", "<");
            strSaved = strSaved.Replace("&gt;", ">");
            strSaved.Append("}");
            using (var stream = new FileStream(Server.MapPath("~/Content/JsonHelper/Test.cs"), FileMode.Truncate))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(strSaved);
                }
            }

            //using (System.IO.StreamWriter sw = System.IO.File.CreateText(Server.MapPath("~/Content/JsonHelper/Test.cs")))
            //{
               
            //    sw.WriteLine(resultClass);
            //} 
            return PartialView("JsonClass", "SUCCESS");
        }


        [HttpPost]
        public ActionResult JsonClass()
        {
            resultClass.Clear();
            StringBuilder strClass = new StringBuilder();
            string jsonText = Request.Form["jText"];//"{'a': '1','c': {'c1':'11'}, 'b': '2', 'd': [{'d1':'1'},{'d1':'2'}]}";
            JContainer content = JObject.Parse(jsonText);
            getClass(content, "TEST");
            //return resultClass.ToString();

            return PartialView("JsonClass", resultClass.ToString());
        }

     
        private void getClass(JContainer j, string Name)
        {
            
            StringBuilder strClass = new StringBuilder();
            strClass.Append("public class "+Name+"<br/>");
            strClass.Append("{<br/>");
            if (j.Children().Count() > 0) {
                foreach (JProperty item in j.Children())
                {
                    if (item.Value.Count() > 0)
                    {
                        if (item.Value.ToString()[0]=='[')
                        {
                            JArray ja = JArray.Parse(item.Value.ToString());
                            getClass((Newtonsoft.Json.Linq.JContainer)(ja[0]), item.Name.ToUpper());
                            strClass.Append("&nbsp;&nbsp;&nbsp;&nbsp;public List" + Server.HtmlEncode("<"+item.Name.ToUpper()+">") + " _o" + item.Name.ToLower() + " { get; set; }<br/>");
                        }
                        else 
                        {
                            getClass((Newtonsoft.Json.Linq.JContainer)(item.Value), item.Name.ToUpper());
                            strClass.Append("&nbsp;&nbsp;&nbsp;&nbsp;public " + item.Name.ToUpper() + " _o" + item.Name.ToLower() + " { get; set; }<br/>");
                        }
                        
                        
                    }
                    else 
                    {
                        strClass.Append("&nbsp;&nbsp;&nbsp;&nbsp;public string " + item.Name + " { get; set; }<br/>");
                    }
                    
                }
                
            }
            strClass.Append("}<br/>");

            resultClass.Append(strClass.ToString());
            resultClass.Append("<br/>");
        }

    }
}
